/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package alcatelparser.modules;

import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import alcatelparser.tags.*;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author geniuz
 */
public class CSVHeaderBuilder {
    public CSVHeaderBuilder()
    {
        _firstRow = true;
        _result = new StringBuffer();
    }

    public final void constructOverallHeader() throws IllegalArgumentException, IllegalAccessException
    {
        _tagID2DecodeValueMap = new LinkedHashMap();
        Field afield[] = ITags.class.getFields();
        Field afield1[] = afield;
        int i = afield1.length;
        for(int j = 0; j < i; j++)
        {
            Field field = afield1[j];
            String s = (String)field.get(field);
            String s1 = field.getName();
            if(!s1.startsWith(I.I(1186)) && (DefaultParserTag.isTagChecked(s) || s.startsWith(I.I(8605))))
                appendTag(s);
        }

        printHeader();
    }

    private void printHeader()
    {
        if(Env.getEnv().isRecordNumbering())
            _result.append(I.I(8716));
        Iterator iterator = _tagID2DecodeValueMap.keySet().iterator();
        printIterator(iterator, false);
        _result.append(I.I(4062));
        if(Env.getEnv().isRecordNumbering())
            _result.append(I.I(8716));
        _firstRow = false;
        iterator = _tagID2DecodeValueMap.keySet().iterator();
        printIterator(iterator, true);
        _result.append(I.I(4062));
    }

    private void printIterator(Iterator iterator, boolean flag)
    {
        while(iterator.hasNext()) 
        {
            LinkedList linkedlist = (LinkedList)(flag ? _tagID2DecodeValueMap.get(iterator.next()) : iterator.next());
            Iterator iterator1 = linkedlist.iterator();
            while(iterator1.hasNext()) 
                _result.append((new StringBuilder()).append(iterator1.next()).append(I.I(8716)).toString());
        }
    }

    private void appendTag(String s)
    {
        boolean flag = Env.getEnv().isFieldsDecoding();
        LinkedList linkedlist = (LinkedList)(flag ? getDecodingTag(s) : getSimpleTag(s, true));
        LinkedList linkedlist1 = (LinkedList)(flag ? getDecodingArrayList(s) : getSimpleTag(s, false));
        if(linkedlist != null)
            _tagID2DecodeValueMap.put(linkedlist, linkedlist1);
    }

    private Object getSimpleTag(String s, boolean flag)
    {
        LinkedList linkedlist = new LinkedList();
        if(s.startsWith(I.I(8605)) && s.equals(I.I(6823)))
        {
            if(flag)
            {
                int i = Env.getEnv().getNumberOfSS() * getSupplementaryMultiplier();
                for(int j = 0; j < i; j++)
                    linkedlist.add("");

            } else
            {
                setupSupplementeryServiceHeader(linkedlist);
            }
            return linkedlist;
        }
        if(!I.I(6881).equals(s))
        {
            linkedlist.add(flag ? ((Object) (s)) : "");
            return linkedlist;
        } else
        {
            return null;
        }
    }

    private String getSupplementaryContent(boolean flag, int i)
    {
        if(DefaultParserTag.isTagChecked(I.I(6823)) && DefaultParserTag.isTagChecked(I.I(6881)))
            return flag ? (new StringBuilder()).append(I.I(6823)).append(i).toString() : (new StringBuilder()).append(I.I(6881)).append(i).toString();
        if(DefaultParserTag.isTagChecked(I.I(6881)))
            return (new StringBuilder()).append(I.I(6881)).append(i).toString();
        else
            return (new StringBuilder()).append(I.I(6823)).append(i).toString();
    }

    private int getSupplementaryMultiplier()
    {
        if(DefaultParserTag.isTagChecked(I.I(6881)) && DefaultParserTag.isTagChecked(I.I(6823)))
            return 2;
        return !DefaultParserTag.isTagChecked(I.I(6881)) && !DefaultParserTag.isTagChecked(I.I(6823)) ? 0 : 1;
    }

    private LinkedList getDecodingTag(String s)
    {
        LinkedList linkedlist = new LinkedList();
        Map map = (Map)Env.getEnv().get(I.I(273));
        if(map.containsKey(s))
        {
            int i = getType(s);
            if(isIncludedTimeTag(i))
            {
                int k = getNumberOfIncludedTags(i);
                if(k == 1)
                {
                    linkedlist.add(s);
                } else
                {
                    linkedlist.add(s);
                    for(k--; k > 0; k--)
                        linkedlist.add("");

                }
            }
        } else
        if(s.startsWith(I.I(8605)) && s.equals(I.I(6823)))
        {
            int j = Env.getEnv().getNumberOfSS() * getSupplementaryMultiplier();
            for(int l = 0; l < j; l++)
                linkedlist.add("");

        } else
        {
            if(s.equals(I.I(6881)))
                return null;
            linkedlist.add(s);
        }
        return linkedlist;
    }

    private LinkedList getDecodingArrayList(String s)
    {
        LinkedList linkedlist = new LinkedList();
        Map map = (Map)Env.getEnv().get(I.I(273));
        if(map.containsKey(s))
            populateTypeList(linkedlist, getType(s));
        else
        if(s.startsWith(I.I(8605)) && s.equals(I.I(6823)))
            setupSupplementeryServiceHeader(linkedlist);
        else
            linkedlist.add("");
        return linkedlist;
    }

    private void setupSupplementeryServiceHeader(List list)
    {
        int i = getSupplementaryMultiplier();
        boolean flag = i == 2;
        int j = Env.getEnv().getNumberOfSS() * i;
        boolean flag1 = true;
        int k = 1;
        boolean flag2 = false;
        for(int l = 0; l < j; l++)
        {
            list.add(getSupplementaryContent(flag1, k));
            if(!flag)
                k++;
            else
            if(flag && flag2)
                k++;
            flag2 = !flag2;
            flag1 = !flag1;
        }

    }

    private void populateTypeList(LinkedList linkedlist, int i)
    {
        if(i == 4)
        {
            linkedlist.add(ReducedFormBuilder.getTypeString(i));
            linkedlist.add(ReducedFormBuilder.getTypeString(5));
        } else
        if(i == 7)
        {
            if(DefaultParserTag.isTagChecked(I.I(5782)))
                linkedlist.add(ReducedFormBuilder.getTypeString(6));
            if(DefaultParserTag.isTagChecked(I.I(5839)))
                linkedlist.add(ReducedFormBuilder.getTypeString(7));
            if(DefaultParserTag.isTagChecked(I.I(5874)))
                linkedlist.add(ReducedFormBuilder.getTypeString(8));
        } else
        if(i == 6)
        {
            if(DefaultParserTag.isTagChecked(I.I(5782)))
                linkedlist.add(ReducedFormBuilder.getTypeString(6));
            if(DefaultParserTag.isTagChecked(I.I(5874)))
                linkedlist.add(ReducedFormBuilder.getTypeString(8));
        }
    }

    private int getType(String s)
    {
        Map map = (Map)Env.getEnv().get(I.I(273));
        return ((Integer)map.get(s)).intValue();
    }

    private boolean isIncludedTimeTag(int i)
    {
        if(i == 6)
            return DefaultParserTag.isTagChecked(I.I(5874)) || DefaultParserTag.isTagChecked(I.I(5782));
        if(i != 7)
            return true;
        else
            return DefaultParserTag.isTagChecked(I.I(5874)) || DefaultParserTag.isTagChecked(I.I(5782)) || DefaultParserTag.isTagChecked(I.I(5839));
    }

    private int getNumberOfIncludedTags(int i)
    {
        int j = 0;
        if(i == 7)
        {
            if(DefaultParserTag.isTagChecked(I.I(5874)))
                j++;
            if(DefaultParserTag.isTagChecked(I.I(5782)))
                j++;
            if(DefaultParserTag.isTagChecked(I.I(5839)))
                j++;
        } else
        if(i == 4)
            j = 2;
        else
        if(i == 6)
        {
            if(DefaultParserTag.isTagChecked(I.I(5874)))
                j++;
            if(DefaultParserTag.isTagChecked(I.I(5782)))
                j++;
        }
        return j;
    }

    public final String getHeader()
    {
        return _result.toString();
    }

    private boolean _firstRow;
    private StringBuffer _result;
    private LinkedHashMap _tagID2DecodeValueMap;

}
